package com.diandian.backend.eleme.retail.exchange.service;

import com.alibaba.fastjson.JSON;
import com.diandian.backend.eleme.retail.exchange.req.SkuStockUpdateOneReq;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.eleme.retail.boot.starter.dto.SkuStockUpdateOneDTO;
import com.diandian.eleme.retail.boot.starter.response.ElemaResponseQueryShop;
import com.diandian.eleme.retail.boot.starter.response.ElemaResponseQueryShopData;
import com.diandian.eleme.retail.boot.starter.response.ElemaResponseSkuListDataList;
import com.diandian.eleme.retail.boot.starter.response.ElemaResponseSkuStockUpdateOne;
import com.diandian.eleme.retail.boot.starter.service.ElemaMerchantShopManageService;
import com.diandian.eleme.retail.boot.starter.service.ElemaSkuManageService;
import com.diandian.eleme.retail.boot.starter.service.ElemaSkuStockManageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class BusinessSkuStockManageService {

    private static final Logger logger = LoggerFactory.getLogger(BusinessSkuStockManageService.class);

    @Autowired
    private ElemaSkuStockManageService skuStockManageService;

    @Autowired
    private ElemaMerchantShopManageService merchantShopManageService;

    @Autowired
    private ElemaSkuManageService skuManageService;

    /**
     * 单个修改商品库存
     * @param data
     * @throws Exception
     */
    public ElemaResponseSkuStockUpdateOne skuStockUpdateOne(String source, String secret, SkuStockUpdateOneReq data) throws Exception {

        // 1. 参数检查
        if (!data.check()) {
            logger.info("更新单个库存时参数缺失，数据：{}", JSON.toJSONString(data));
            throw new DefaultException(1, "更新单个库存时参数缺失");
        }

        // 2. 查询线上门店是否存在，避免饿了么官方统计为异常请求
        ElemaResponseQueryShopData onlineStoreData = null;
        try {
            ElemaResponseQueryShop elemaResponseQueryShop = merchantShopManageService.shopGet(source, secret, data.getStoreId(), null);
            if(elemaResponseQueryShop != null && "0".equals(elemaResponseQueryShop.getErrno()) && elemaResponseQueryShop.getData() != null){
                onlineStoreData = elemaResponseQueryShop.getData();
            }
        } catch (Exception e) {
            logger.error("单个修改商品库存时出错，门店编码：" + data.getStoreId(), e);
        }
        if(onlineStoreData == null || onlineStoreData.getShop_id() == null || "".equals(onlineStoreData.getShop_id())){
            logger.info("单个修改商品库存时出错，门店{}信息不存在", data.getStoreId());
            throw new DefaultException("单个修改商品库存时出错，门店" + data.getStoreId() + "{}信息不存在");
        }

        // 3. 查询线上商品是否存在，避免饿了么官方统计为异常请求
        List<ElemaResponseSkuListDataList> skuListDataList = skuManageService.skuList(source, secret, data.getStoreId(), data.getProductId());
        if(skuListDataList == null || skuListDataList.isEmpty()){
            throw new DefaultException("单个修改商品库存时出错，门店" + data.getStoreId() + "不存在商品" + data.getProductId());
        }

        // 4. 构造请求数据
        SkuStockUpdateOneDTO dataDTO = new SkuStockUpdateOneDTO();
        dataDTO.setShopId(data.getStoreId());
        dataDTO.setCustomSkuId(data.getProductId() + ":" + data.getStockCount());

        // 5. 执行API调用
        return skuStockManageService.skuStockUpdateOne(source, secret, dataDTO);
    }
}
